package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/tag")
public class TagController extends BaseController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index (){
        return "tag/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add (ModelMap modelMap){
        modelMap.put("action", "/tag/add");
        modelMap.put("tag", new Tag());
        return "tag/add";
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info (String id, ModelMap modelMap){
        modelMap.put("tag", tagService.getById(id));
        return "tag/info";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update (String id, ModelMap modelMap){
        modelMap.put("action", "/tag/update");
        modelMap.put("tag", tagService.getById(id));
        return "tag/add";
    }

    @RequestMapping(value = "/getByPage")
    @ResponseBody
    public Map getByPage (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<Tag> list = tagService.getByPage((pno-1)*psize, psize);
        return result(list, tagService.getCount(), "");
    }

    @RequestMapping(value = "/getById")
    @ResponseBody
    public Tag getById (String id){
        return tagService.getById(id);
    }

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Map getAll (){
        List<Tag> list = tagService.getAll();
        return result(list, list.size(), "");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map add (Tag tag){
        tagService.add(tag);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update (Tag tag){
        tagService.update(tag);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        tagService.delete(id);
        return result(SUCCESS, "操作成功");
    }
}
